using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Umbraco.Ecommerce.BusinessLogic;

namespace Umbraco.Ecommerce.WebShop.UserControls
{
    public partial class AddProduct : System.Web.UI.UserControl
    {
        umbraco.presentation.nodeFactory.Node n;
        Product p;

        protected void Page_Load(object sender, EventArgs e)
        {
            n = umbraco.presentation.nodeFactory.Node.GetCurrent();
            p = new Product(n.Id);
            BindData();

            if (umbraco.library.GetDictionaryItem("addtocart") != "")
                AddtoCart.Text = umbraco.library.GetDictionaryItem("addtocart");

            if (umbraco.library.GetDictionaryItem("description") != "")
                description.Text = umbraco.library.GetDictionaryItem("description");

            if (umbraco.library.GetDictionaryItem("quantity") != "")
                headerquantity.Text = umbraco.library.GetDictionaryItem("quantity");

            if (umbraco.library.GetDictionaryItem("price") != "")
                headerprice.Text = umbraco.library.GetDictionaryItem("price");
        }
        private void BindData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("price");
            dt.Columns.Add("id");

            if (p.UseMultipleProductVariants)
                foreach (ProductVariant pv in p.ProductVariants)
                {
                    DataRow dr = dt.NewRow();
                    dr["name"] = n.Name + " - " + pv.Name;
                    dr["price"] = Library.FormattedPrice(pv.Price);
                    dr["id"] = pv.Id;
                    dt.Rows.Add(dr);
                }
            else
            {
                foreach (ProductVariant pv in p.ProductVariants)
                {
                    DataRow dr = dt.NewRow();
                    dr["name"] = n.Name;
                    dr["price"] = Library.FormattedPrice(pv.Price);
                    dr["id"] = pv.Id;
                    dt.Rows.Add(dr);
                }
            }

            variantRepeater.DataSource = dt;
            variantRepeater.DataBind();
        }

        protected void AddtoCart_Click(object sender, EventArgs e)
        {
            foreach (ProductVariant pv in p.ProductVariants)
            {
                int qty = int.Parse(Request.Form["qty_variantId_" + pv.Id]);
                if (qty > 0)
                {
                    Library.AddProductToCart(pv.ProductId, qty);
                }
            }

            string currenturl = umbraco.library.NiceUrl(n.Id);
            string carturl;
            if (umbraco.GlobalSettings.UseDirectoryUrls)
            {
                carturl = currenturl + "/cart";
            }
            else
            {
                carturl = currenturl.Replace(".aspx", "/cart.aspx");
            }

            Response.Redirect(carturl);
        }
    }
}